//
//  AccountCore.m
//  HHNMobileCore
//
//  Created by huhmf on 16/1/14.
//  Copyright © 2016年 huhmf. All rights reserved.
//

#import "AccountCore.h"
#import "UserInfoRequest.h"
#import <MJExtension.h>
@implementation AccountCore

// 获取用户详细信息
-(void) requestUserInfo:(NSString *) userName SuccBlock:(void(^)(UserInfo *userInfo)) succBlock andFailBlock:(void(^)(NSString *msg))failBlock{
    
    
    UserInfoRequest* api = [[UserInfoRequest alloc] initRequestWithUserName:userName];
    [api startWithCompletionBlockWithSuccess:^(YTKBaseRequest *request) {
        NSError *err;
        HHLog(@"result = %@",request.responseString);
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:request.responseString.mj_JSONData
                                                             options:NSJSONReadingMutableContainers
                                                               error:&err];
        if(err) {
            LogError(THome, @"requestProductListApi,json to dict error：%@", err.localizedDescription);
            failBlock([NSString stringWithFormat:@"json to dict error：%@",err.localizedDescription]);
            return;
        }
        
        
        succBlock([self dictionaryToUserInfo:dict]);
        
    } failure:^(YTKBaseRequest *request) {
        LogError(THome, @"requestProductListApi error:%@", request.requestTask.error.localizedDescription);
        failBlock(request.requestTask.error.localizedDescription);
    }];

    
}

-(UserInfo *) dictionaryToUserInfo:(NSDictionary*) dict {
    
    if (dict==nil) {
        return nil;
    }
    
    if([[dict valueForKey:@"data"] isKindOfClass:[NSDictionary class]])
    {
        NSDictionary *data_dict = [dict valueForKey:@"data"];
        
        UserInfo *list=[UserInfo mj_objectWithKeyValues:data_dict];
        return list;
        
        
    }else{
        LogError(THome, @"dictionary to object error:data is not dictionary");
        return nil;
    }
    
    
}
@end
